﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OUClampAngle : MonoBehaviour
{
    public float hor = 1;
    public float speed = 20f;

    public float anglerMax = 30;
    public float anglerMin = 30;
    private float incrementAngler = 0;
    private bool b = false;

    private float curMax = 0;
    private float curMin = 0;

    public bool isState = false;
    
    public void Awake()
    {
        curMax = transform.rotation.eulerAngles.y + anglerMax;
        curMin = transform.rotation.eulerAngles.y + anglerMin;
    }

    private void FixedUpdate()
    {
        if (isState)
        {
            RotSus();
        }
    }

    private void RotSus()
    {
        if (incrementAngler >= curMax)
        {   
            b = false;
            Debug.Log("max angle " + incrementAngler);
        }
        if (incrementAngler <= curMin)
        {
            b = true;
            Debug.Log("min angle " + incrementAngler);
        }

        if (b)
        {
            incrementAngler += speed * Time.fixedDeltaTime;    
        }
        else
        {
            incrementAngler -= speed * Time.fixedDeltaTime;    
        }

        var ea = transform.rotation.eulerAngles;
        ea.y = incrementAngler;
        transform.rotation = Quaternion.Euler(ea);
    }

}
